package com.xiaobai.service.addition;

import com.alibaba.fastjson.JSON;
import com.xiaobai.bo.FreeClassroomAll;
import com.xiaobai.config.MyJSESSIONIdManager;
import com.xiaobai.service.oss.IOSSService;
import com.xiaobai.units.FreeClassroomConvert;
import com.xiaobai.units.FreeClassroomUtil;
import com.xiaobai.units.HttpUnits_V2;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Date;
import java.util.Map;

/**
 * @description:
 * @author: 小白白
 * @create: 2021-11-26
 **/

@Service
@Slf4j
public class FreeClassroomService implements IFreeClassroomService {

    private static final String CDNPreUrl = "http://image.xiaobaibai.work/freeClassroom/";
    private static final String CDNPreUrlHTTPS = "https://image.xiaobaibai.work/freeClassroom/";
    private static final String freeClassroomUrl = "https://jwxt.gdupt.edu.cn/teajssqxx!getPlJsDataList.action";

    @Value("${baseStudentId}")
    private String baseStudentId;

    @Value("${baseStudentPassword}")
    private String baseStudentPassword;

    @Autowired
    private IOSSService qiNiuService;

    @Override
    public boolean exist(Date date) {
        String fileName = DateFormatUtils.format(date, "yyyy-MM-dd") + ".json";
        String url = CDNPreUrl + fileName;
        Integer status = null;
        try {
            status = HttpUnits_V2.getStatus(url);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return status != null && status.equals(200);
    }

    @Override
    public void save(Date[] dates) {

        if (dates == null || dates.length == 0){
            log.info("日期数组为0,不更新freeClassroom");
            return;
        }

        // 教务系统登陆后才可以请求空闲教室API
        HttpUnits_V2.gyLogin(baseStudentId, baseStudentPassword);
        String fileName = null;
        for (Date date : dates) {
            // 每天5份
            String[] arr = new String[5];
            for (int i = 0; i < 5; i++) {
                Map<String, Object> paramsMap = FreeClassroomUtil.buildParamsMap(date, new int[]{(i + 1)});
                String content = HttpUnits_V2.commonGyPost(freeClassroomUrl, paramsMap, null);
                arr[i] = content;
            }
            // 清洗并转换数据,得到每日的空闲教室对象
            FreeClassroomAll freeClassroomAll = FreeClassroomConvert.convert(arr);
            String json = JSON.toJSONString(freeClassroomAll);
            byte[] bytes = json.getBytes();
            fileName = DateFormatUtils.format(date, "yyyy-MM-dd") + ".json";
            // 保存至七牛云
            qiNiuService.uploadContent(bytes, fileName);
            // 七牛云刷新CDN [http://image.xiaobaibai.work/freeClassroom/] 刷新每一天
            qiNiuService.refreshByFileCDN(CDNPreUrl + fileName);
            qiNiuService.refreshByFileCDN(CDNPreUrlHTTPS + fileName);
        }
        MyJSESSIONIdManager.remove();
        // 删除昨天的空闲教室json文件
        qiNiuService.deleteFile(DateFormatUtils.format(DateUtils.addDays(new Date(), -1), "yyyy-MM-dd") + ".json");
    }

}
